package com.qch.edu.lagou.datastructure.hashmap;

/**
 * @Author qch
 * @Date 2021/7/13
 * 单链表
 */
public class ListNode {
    Node head;//头结点

    /**
     * 添加单链表节点
     *
     * @param key
     * @param value
     */
    public void addNode(String key, String value) {
        //在外界设置好head了
        if (head == null) return;
        Node node = new Node(key, value, null);
        Node tmp = head;
        //循环单链表
        while (true) {
            if (key.equals(tmp.key)) {
                tmp.value = value;
            }
            if (tmp.next == null) {
                break;
            }
            tmp = tmp.next;
        }
        //挂载节点
        tmp.next = node;

    }

    /**
     * 获得值
     *
     * @param key
     * @return
     */
    public String getVal(String key) {
        if (head.next == null) {
            return head.value;
        } else {//遍历单链表
            Node tmp = head;
            while (tmp != null) {
                if (key.equals(tmp.key)) {
                    return tmp.value;
                }
                //指向先提供
                tmp = tmp.next;
            }
        }
        return null;
    }
}
